#include "Timer.h"





CTimer::CTimer(): _startTime(0.0f), _currentTime(0.0f), _isPerformance(true)
{
	memset(&_timer, 0, sizeof(_timer));

	if(!QueryPerformanceFrequency((LARGE_INTEGER *) &_timer.frequency))
	{
		_timer.performance_timer	= false;
		_timer.timer_start			= timeGetTime();
		_timer.resolution			= 1.0f / 1000.0f;
		_timer.frequency			= 1000;
		_timer.timer_elapsed		= _timer.timer_start;
	}
	else
	{
		QueryPerformanceCounter((LARGE_INTEGER*) &_timer.ptimer_start);
		_timer.performance_timer	= true;
		_timer.resolution			= (float)(((double)1.0f)/(double)_timer.frequency);
		_timer.ptimer_elapsed		= _timer.ptimer_start;
	}
}


float CTimer::GetTime(void)
{
	__int64 time;

	if(_timer.performance_timer)
	{
		QueryPerformanceCounter((LARGE_INTEGER*) &time);
		return ((float)(time - _timer.ptimer_start) * _timer.resolution);
	}
	else
	{
		return ((float)(timeGetTime() - _timer.timer_start) * _timer.resolution);
	}
}


void CTimer::Start()
{
	_startTime = this->GetTime();
}


float CTimer::Get() const
{
	return _currentTime;
}


void CTimer::Stop()
{
	_currentTime = this->GetTime() - _startTime;
	_startTime = 0.0f;
}


void CTimer::Reset()
{
	this->Start();
}